//
//  PreviewViewController.m
//  UEAssistant
//
//  Created by mac on 2017/2/7.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "PreviewViewController.h"
#import "PreViewTableViewCell.h"
#import "PreViewModel.h"

@interface PreviewViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSMutableArray                      *_dataSource;
    NSInteger                           _pageNumber;
    BOOL                                _isLoadIOver;
    
}

@property (nonatomic, strong) UITableView     *tableView;
@property (nonatomic, strong) NSDictionary    *param;

@end

@implementation PreviewViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initlizeUserface];
    [self initlizeDataSource];
}

- (void)initlizeUserface{
    
    if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
        self.automaticallyAdjustsScrollViewInsets = YES;
    }
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"查看原文";
    [self.view addSubview:self.tableView];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopic)];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    [self setExtraCellLineHidden:self.tableView];
    
}

- (void)initlizeDataSource{
    
    [LSProgressHUD showLoadingToView:self.view];
    _pageNumber = 1;
    _dataSource = [NSMutableArray array];
    NSString *string = [NSString stringWithFormat:@"%ld",(long)_pageNumber];
    if (orderSingleton.segementIndex == 0) {
        _param = @{@"plattask_id":self.plattask_id,
                   @"pageSize":@"30",
                   @"pageNum":string};
    }else{

        _param = @{@"sectorId":self.sectorId,
                   @"pageSize":@"30",
                   @"pageNum":string};
    }

    [AvailableNetWorking postWithUrl:_connector params:_param successHandler:^(id responseObject) {
        
        TGLog(@"%@",responseObject[@"message"]);
        [LSProgressHUD hiddenLoading:self.view];
        NSArray *arr = responseObject[@"result"];
        if (arr.count == 0) {
            
        }else{
            for (int i = 0; i < ((NSArray *)responseObject[@"result"]).count; i++) {
                NSDictionary *dic = ((NSArray *)responseObject[@"result"])[i];
                PreViewModel *model = [[PreViewModel alloc]initWithDictionary:dic];
                [_dataSource addObject:model];
            }
            [self.tableView reloadData];
            [LSProgressHUD hiddenLoading:self.view];
        }
    } falureHandler:^(NSError *error) {
        [LSProgressHUD hiddenLoading:self.view];
    }];
}
- (void)loadMoreTopic{
    
    _pageNumber += 1;
    NSString *string = [NSString stringWithFormat:@"%ld",(long)_pageNumber];
    if (orderSingleton.segementIndex == 0) {
        _param = @{@"plattask_id":self.plattask_id,
                   @"pageSize":@"30",
                   @"pageNum":string};
    }else{
        _param = @{@"sectorId":self.sectorId,
                   @"pagesize":@"30",
                   @"pagenum":string};
    }
    
    [AvailableNetWorking postWithUrl:_connector params:_param successHandler:^(id responseObject) {
        TGLog(@"pagenum =========== %@",string);
        NSDictionary *pageInfo = responseObject[@"pageinfo"];
        TGLog(@"%@",responseObject[@"message"]);
        if ([pageInfo[@"pagecount"] intValue] < (_pageNumber - 1)*30) {
            [TopAlertView SetUpbackgroundColor:mainColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:@"已经加载完全部数据" andTitleColor:[UIColor whiteColor] andCancel:NO];
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            _pageNumber -= 1;
            _isLoadIOver = YES;
            return;
        }else{
            for (int i = 0; i < ((NSArray *)responseObject[@"result"]).count; i++) {
                NSDictionary *dic = ((NSArray *)responseObject[@"result"])[i];
                PreViewModel *model = [[PreViewModel alloc]initWithDictionary:dic];
                [_dataSource addObject:model];
            }
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
        }
    } falureHandler:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PreViewTableViewCell *cell = [PreViewTableViewCell cellWithTableview:tableView];
    cell.model = _dataSource[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return ((PreViewModel *)_dataSource[indexPath.row]).cellHeight;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.y + (scrollView.frame.size.height) > scrollView.contentSize.height && !_isLoadIOver) {
        [self.tableView.mj_footer beginRefreshing];
    }
}

#pragma mark -- 隐藏空白cell的分割线
-(void)setExtraCellLineHidden: (UITableView *)tableView{
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

#pragma getter
- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64) style:UITableViewStylePlain];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = RGB(241, 241, 241, 1);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.scrollEnabled = YES;
        _tableView.bounces = YES;
    }
    return _tableView;
}


@end

